<?php
class b2c_tasks_sostatussync extends base_task_abstract implements base_interface_task{
public function exec($params=null){
	$model=app::get('b2c')->model('orders');
	//单据已同步到U8,但付款状态未同步,付款状态1已支付2已付担保3部分付
	$data=$model->getList('order_id,(case pay_status when "1" then "已支付" when "2" then "已付款至到担保方" when "3" then "部分付款" end) as pay_status,"是" as sender',array('is_synchronization'=>'true','is_status_sync'=>0,'pay_status|in'=>array('1','2','3')),0,-1,'createtime');	
	$wsdl=U8_SOSTATUSWSDL;
	try{
		$client = new SoapClient($wsdl);
	}catch(exception $ex){
		$this->writeLog($ex->getMessage(),'实例化请求地址时出错,请检查');
		return;
	}
	//循环调用接口
	foreach($data as $value){
		try{
			$output = $client->changestatus($value['order_id'],$value['pay_status'],$value['sender']);
		}catch(exception $ex){
			$this->writeLog($ex->getMessage(),'付款状态同步异常');
			continue;
		}
		//$response_datas=json_decode($output,true);
		//返回值处理
		if ($output == 'ok'){
			//如果是部分付款,则不更新同步状态
			if ($value['pay_status']=='部分付款') continue;
			//更新订单付款状态的同步状态
			$syncData=array('is_status_sync'=>1);
			$model->update($syncData,array('order_id'=>$value['order_id']));	
		}
		else{
			$this->writeLog($output,'付款状态同步异常',$value['order_id']);
		}
	}
}

//写日志
private function writeLog($failureReason,$memo='',$docno=0){
$model1 = app::get('b2c')->model('synerrorlog');
            $data1 = array(
              'CreateTime'=>time(),
              'CrontabId'=>'b2c_tasks_sostatussync',
              'DocNo'=>$docno ,
              'Issyn'=>'false' ,
              'FailureReason'=> $failureReason ,
              'Memo' =>$memo,
              );
$model1->insert($data1);
}
 
}   
